<?php

  // Define  
  defined ( 'REST_CODE_LANG' ) || define ( 'REST_CODE_LANG', 'code' );
  defined ( 'REST_TYPE_LANG' ) || define ( 'REST_TYPE_LANG', 'type' );
  defined ( 'REST_VALUE_LANG' ) || define ( 'REST_VALUE_LANG', 'value' );
  defined ( 'REST_TOKEN_LANG' ) || define ( 'REST_TOKEN_LANG', 'token' );

  // Define 
  defined ( 'REST_CODE' ) || define ( 'REST_CODE', - 999 );
  defined ( 'REST_TYPE' ) || define ( 'REST_TYPE', 'ERROR' );
  defined ( 'REST_VALUE' ) || define ( 'REST_VALUE', 'UNKNOW ERROR' );
  defined ( 'REST_TOKEN' ) || define ( 'REST_TOKEN', null );

  /**
  * @Copyright 2011 SGame
  * @author tientm
  */
  class DSException extends exception {

    private $_err_code;
    private $_err_type;
    private $_err_msg;
    private $_err_token;

    public function __construct($err_code = REST_CODE, $err_type = REST_TYPE, $err_msg = REST_VALUE, $err_token = REST_TOKEN) {
      $this->_err_code = $err_code;
      $this->_err_type = $err_type;
      $this->_err_msg = $err_msg;
      $this->_err_token = $err_token;

      parent::__construct ( $this->_err_msg, $this->_err_code );
    }

    public function getErrCode() {
      return $this->_err_code;
    }

    public function getErrType() {
      return $this->_err_type;
    }

    public function getErrMsg() {
      return $this->_err_msg;
    }

    public function __toString() {
      $str = $this->_err_type . ': ';
      if ($this->_err_code != 0) {
        $str .= $this->_err_code . ': ';
      }
      return $str . $this->_err_msg . "\n";
    }

    public function __toJson() {
      return json_encode ( array (REST_CODE_LANG => $this->_err_code, REST_TYPE_LANG => $this->_err_type, REST_VALUE_LANG => $this->_err_msg, REST_TOKEN_LANG => $this->_err_token ) ) . "\n";
    }

    public function __toArray() {
      return array (REST_CODE_LANG => $this->_err_code, REST_TYPE_LANG => $this->_err_type, REST_VALUE_LANG => $this->_err_msg, REST_TOKEN_LANG => $this->_err_token );
    }


    /**
    * Return json data
    * 
    * @author tientm
    * @param string $string
    * @return json
    */
    public static function setStringResult($token = null, $value = null) {
      return array (REST_CODE_LANG => 1, REST_TYPE_LANG => 'SUCCESS', REST_VALUE_LANG => $value, 'token' => $token );
    }

    /**
    * Return json data
    * 
    * @author tientm
    * @param object $obj
    * @return json
    */
    public static function setObjectResult($token = array(), $obj = null) {
      return array (REST_CODE_LANG => 1, REST_TYPE_LANG => 'SUCCESS', REST_VALUE_LANG => json_encode ( $obj ), 'token' => $token );
    }

  }
?>
